package com.bishe.psy.controller;

import com.bishe.psy.common.AppResult;
import com.bishe.psy.common.ResultCode;
import com.bishe.psy.config.AppConfig;
import com.bishe.psy.exception.ApplicationException;
import com.bishe.psy.model.AdmUser;

import com.bishe.psy.model.ConUser;
import com.bishe.psy.service.IAdmUserService;
import com.bishe.psy.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: L
 * Date: 2024-03-28
 * Time: 17:45
 */

@Slf4j
@Api(tags = "管理员用户接口")
@RestController //  = @ResponseBody:返回非页面数据 + @Controller :让spring框架启动时加载
@RequestMapping("/adm") // 一级路由映射
public class AdmUserController {

    @Resource
    private IAdmUserService iAdmUserService;

    //password、passwordRepeat都是明文密码
    @ApiOperation("管理员登录方法")
    @PostMapping("/login") // 二级路由映射
    public AppResult login (HttpServletRequest request,
                            @ApiParam(value = "用户名")  @RequestParam("username") String username,
                            @ApiParam(value = "密码") @RequestParam("password") String password) {

        //System.out.println("-------------------------");
      //  System.out.println(username);
      //  System.out.println(password);
      //  System.out.println(identity);
      //  System.out.println("-------------------------");

        //1.参数校验
        if(StringUtil.isEmpty(username) || StringUtil.isEmpty(password)) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            return AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE);
        }

        //2.调用service
        AdmUser admUser = iAdmUserService.login(username, password);

        //登录成功，把User对象设置到session作用域中
        HttpSession session = request.getSession(true);
        session.setAttribute(AppConfig.USER_SESSION,admUser);

        return AppResult.success();
    }

    @ApiOperation("获取管理员信息")
    @GetMapping("/adminfo")
    public AppResult<AdmUser> getStuUserinfo(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        //校验通过，取出当前用户信息
        AdmUser admUser = (AdmUser) session.getAttribute(AppConfig.USER_SESSION);
        return AppResult.success(admUser);
    }

    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public AppResult logout(HttpServletRequest request) {

        //获取session对象
        HttpSession session = request.getSession(false);
        if(session != null) { //表示当前对象已登录
            log.info("退出成功");
            session.invalidate();
        }
        return AppResult.success("退出成功");
    }


    @ApiOperation("管理员修改个人信息接口")
    @PostMapping("/modifyAdm")
    public AppResult modifyAdm(HttpServletRequest request,
            @ApiParam(value = "用户昵称") @RequestParam(value = "nickname",required = false) String nickname) {

        System.out.println(nickname);
        //1.参数校验，当传入的数据都为空时，异常
        if(StringUtil.isEmpty(nickname)) {
            return AppResult.failed("请输入要修改的内容");
        }

        //2.获取登录信息
        HttpSession session = request.getSession(false);
       AdmUser admUser = (AdmUser) session.getAttribute(AppConfig.USER_SESSION);

       //3.封装对象
        AdmUser updateAdm = new AdmUser();
        updateAdm.setId(admUser.getId());
        updateAdm.setNickname(nickname);

        //4.调用service
        iAdmUserService.modifyAdmInfo(updateAdm);

        //5.查询最新的数据
       admUser = iAdmUserService.selectByPrimaryKey(admUser.getId());

        //6.更新到session中
        session.setAttribute(AppConfig.USER_SESSION,admUser);

        //7.返回数据
        return AppResult.success(admUser);
    }


    @ApiOperation("修改密码接口")
    @PostMapping("/modifyPsw")
    public AppResult modifyPassWord(HttpServletRequest request,
                                    @ApiParam(value = "旧密码")  @RequestParam("oldPassword") String oldPassword,
                                    @ApiParam(value = "新密码")  @RequestParam("newPassword")String newPassword,
                                    @ApiParam(value = "确认密码")  @RequestParam("confirmPassword")String confirmPassword) {

        //1.获取用户信息
        HttpSession session = request.getSession(false);
        AdmUser admUser = (AdmUser) session.getAttribute(AppConfig.USER_SESSION);

        //2.判断确认和新密码是否一样

        if(!newPassword.equals(confirmPassword)) {
            return AppResult.failed(ResultCode.FAILED_TWO_PWD_NOT_SAME);
        }

        //3.调用service
        iAdmUserService.modifyPassword(admUser.getId(),oldPassword,newPassword);

        //4.销毁session
        if(session != null) {
            session.invalidate();
        }
        //5.返回结构
        return AppResult.success();
    }

}
